package com.b1502.store2.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.b1502.store2.R;
import com.b1502.store2.activity.CategoryActivity;
import com.b1502.store2.adapter.CategoryFragmentAdapter;
import com.b1502.store2.model.Category;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.x;

/**
 * A
 * simple {@link Fragment} subclass.
 * Use
 * the
 * {@link CategoryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class
CategoryFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    //提全局变量
    RecyclerView recycler_category;
    CategoryFragmentAdapter categoryFragmentAdapter;
    Category[] categories;

    public CategoryFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CategoryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CategoryFragment newInstance(String param1, String param2) {
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        setWeidgeListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        //网络请求
        getCategories();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment


        return inflater.inflate(R.layout.fragment_category, container, false);
    }


    //初始化界面操作
    public void initView() {

        //初始化recyclerview控件
        recycler_category = (RecyclerView) getView().findViewById(R.id.rvcycler_category);
        //设置布局管理器
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        recycler_category.setLayoutManager(staggeredGridLayoutManager);
        //实例化适配器
        categoryFragmentAdapter = new CategoryFragmentAdapter(getActivity());

    }

    //设置点击事件
    private void setWeidgeListener() {

        categoryFragmentAdapter.setListener(new CategoryFragmentAdapter.Item() {
            @Override
            public void onClick(int position) {
                //Toast.makeText(getActivity(), "条目" + position, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getActivity(), CategoryActivity.class);
                intent.putExtra("name", categories[position].getName());
                intent.putExtra("id", categories[position].getId());
                startActivity(intent);
            }
        });
    }

    //请求数据
    private void getCategories() {

        StoreParams params = new StoreParams(UrlUtil.GetCategories);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {

                //解析的数据封装到数组里面
                Gson gson = new Gson();
                categories = gson.fromJson(result, Category[].class);
                categoryFragmentAdapter.addData(categories);
                recycler_category.setAdapter(categoryFragmentAdapter);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {


            }
        });
    }


}
